package com.study.core.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    @Value("${ibank.security.cors.allowedOrigins:*}")
    private String allowedOrigins;
    @Value("${ibank.security.cors.allowedHeaders:*}")
    private String allowedHeaders;
    @Value("${ibank.security.cors.allowedMethods:*}")
    private String allowedMethods;

    /**
     * 服务端跨域配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")// 匹配的路径
                .allowedOrigins(allowedOrigins.split(","))// 允许的请求源
                .allowedHeaders(allowedHeaders.split(","))// 允许的请求头
                .allowedMethods(allowedMethods.split(","))// 允许的请求方法
                .allowCredentials(true);// 允许发送Cookie

    }

}